package com.rbcs.budget.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.rbcs.budget.model.BudgetItem;
import com.rbcs.budget.service.BudgetItemtManager;

@SuppressWarnings("serial")
public class BudgetItemAmountTag  extends BodyTagSupport {

	private Integer itemId;

	private Integer deptId;

	private Integer year;

	@Override
	public int doEndTag() throws JspException {
		BudgetItem bi = getBudgetItemtManager().getBudgetItem(itemId, deptId,
				year);
		try {
			if (bi != null) {
				pageContext.getOut().print(bi.getAmount());
			}else{
				pageContext.getOut().print(0.0);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private BudgetItemtManager getBudgetItemtManager() {
		return (BudgetItemtManager) getContext().getBean("budgetItemtManager");
	}

	private ApplicationContext getContext() {
		return WebApplicationContextUtils.getWebApplicationContext(pageContext
				.getServletContext());

	}

	public Integer getItemId() {
		return itemId;
	}

	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}

	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}
	
}
